<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <el-row>
        <div class="mb8" style="width: 100%">
          <el-button icon="plus" type="primary"  @click="checkProjectDetailsRef.openDialog()"
                     v-auth="'qms_qmsCheckProjectDetails_add'">
            {{$t('operation.add')}}
          </el-button>
          <el-button  :disabled="multiple" icon="Delete" type="danger"
                     v-auth="'qms_qmsCheckProjectDetails_del'" @click="handleDelete(selectObjs)">
            {{$t('btn.delete')}}
          </el-button>
        </div>
      </el-row>
      <el-table :data="state.dataList" v-loading="state.loading" border
                :cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
                @selection-change="selectionChangHandle" style="width: 100%;height: 100%;"
                @sort-change="sortChangeHandle">
        <el-table-column type="selection" width="40" align="center"/>
        <el-table-column prop="checkTypeName" :label="$t('qms.CheckType')" show-overflow-tooltip>
          <template #default="scope">
            <el-tag :type="getTagAttr(scope.row.checkTypeCode).type" >
              {{ scope.row.checkTypeName }}
            </el-tag>
          </template>
        </el-table-column>/
        <el-table-column prop="checkTypeCode" :label="$t('qms.checkTypeCode')" show-overflow-tooltip>
          <template #default="scope">
            <el-tag :type="getTagAttr(scope.row.checkTypeCode).type" >
              {{ scope.row.checkTypeCode }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column :label="$t('qms.detail')">
        <el-table-column width="200" prop="checkProjectName" :label="$t('qms.testItem')" show-overflow-tooltip/>
        <el-table-column width="200" prop="checkProjectCode" :label="$t('qms.testItemCode')" show-overflow-tooltip/>
        <el-table-column prop="checkFunction" :label="$t('qms.testMethod')" show-overflow-tooltip/>
        <el-table-column prop="checkCondition" :label="$t('qms.testCondition')" show-overflow-tooltip/>
        <el-table-column prop="resultType" :label="$t('qms.resultType')" show-overflow-tooltip/>
        <el-table-column prop="standardItem" :label="$t('qms.standardValue')" show-overflow-tooltip/>
        <el-table-column prop="unit" :label="$t('iot.unit')" show-overflow-tooltip/>
        </el-table-column>
        <el-table-column fixed="right" :label="$t('operation.Operation')" width="150">
          <template #default="scope">
            <el-button icon="edit" type="primary" v-auth="'qms_qmsCheckProjectDetails_edit'"
                       @click="checkProjectDetailsRef.openDialog(scope.row.id)">
            </el-button>
            <el-button icon="delete" type="danger" v-auth="'qms_qmsCheckProjectDetails_del'"
                       @click="handleDelete([scope.row.id])">
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination"/>
    </div>
    <detial-form ref="checkProjectDetailsRef" @refresh="getDataList(false)"/>
  </div>
</template>

<script setup lang="ts">
import {BasicTableProps, useTable} from "/@/hooks/table";
import {delObjs, qmsCheckProjectDetailsPage} from "/@/api/platform/qms/qmsCheckProjectDetails";
import {useMessage, useMessageBox} from "/@/hooks/message";
// import DetialForm from "./detialForm";
import {useI18n} from "vue-i18n";

const {t} = useI18n();

// 引入组件
const detialForm = defineAsyncComponent(() => import('./detialForm.vue'));

const checkProjectDetailsRef = ref()



// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: qmsCheckProjectDetailsPage
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
  tableStyle
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/qms/qmsCheckProjectDetails/export', Object.assign(state.queryForm, {ids: selectObjs}), 'qmsCheckProjectDetails.xlsx')
}

const statusColorMap = new Map([
  ['FQC', { type: 'primary', effect: 'plain' }],
  ['IQC', { type: 'success', effect: 'plain' }],
  ['TI', { type: 'warning', effect: 'plain' }],
  ['PQC', { type: 'info', effect: 'plain' }],
]);
// 通过状态获取tag标签的属性
const getTagAttr = (status) => {
  if(!status){
    return { type: 'primary', effect: 'plain' }
  }
  return statusColorMap.get(status);
};

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({id}) => id);
  multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm(t('operation.permanentDeletion'));
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    getDataList();
    useMessage().success(t('qms.Successful'));
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};
</script>